package com.tb.controller;

import com.github.pagehelper.PageInfo;
import com.tb.model.Illegality;
import com.tb.model.Message;
import com.tb.model.PageBean;
import com.tb.model.User;
import com.tb.service.FollowService;
import com.tb.service.IllegalityService;
import com.tb.service.MessageService;
import com.tb.service.UserService;
import com.tb.util.MyUtils;
import com.tb.util.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 用户相关
 * @author: wangguichun
 * @create: 2018-04-16 16:51
 **/

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
    @Autowired
    private IllegalityService illegalityService;
    @Autowired
    private FollowService followService;


    @RequestMapping("message/{pageId}")
    public String message(@PathVariable Integer pageId, Model model, HttpServletRequest request) {
        int myId = Integer.parseInt(MyUtils.getCookie(request, "id").getValue());
        PageInfo<Message> pageInfo = messageService.getMessageByToIdAndPageId(myId, pageId);
        model.addAttribute(pageInfo);
        return "user/message";
    }


    @RequestMapping("setting")
    public String setting(Model model, HttpServletRequest request) {
        String id = MyUtils.getCookie(request, "id").getValue();
        User user = userService.getUserById(Integer.parseInt(id));
        model.addAttribute("user",user);
        return "user/setting";
    }

    @RequestMapping("updateProfile")
    @ResponseBody
    public void updateProfile(User user) {
        userService.modify(user);
    }

    @RequestMapping("changePsw")
    @ResponseBody
    public Response changePsw(int id, String oldPsw, String newPsw){
        boolean bool = userService.changePsw(id,oldPsw,newPsw);
        if (bool){
            return new Response(0,"修改成功",null);
        }else{
            return new Response(1,"原密码错误",null);
        }
    }

//    @RequestMapping("changeAvatar")
//    public String changeAvatar(int userId, MultipartFile file, HttpServletRequest request) throws IOException {
//        userService.modifyAvatarFile(userId,file,request);
//        return "redirect:setting";
//    }

    @RequestMapping("changeAvatarAjax")
    @ResponseBody
    public Response changeAvatarAjax(int userId, MultipartFile file, HttpServletRequest request) throws IOException {
        return userService.modifyAvatarFile(userId,file,request);
    }

    @RequestMapping("accuse")
    @ResponseBody
    public void accuse(Illegality illegality){
        illegalityService.addRecord(illegality);
    }

    @RequestMapping("userInfo")
    @ResponseBody
    public Map<String,Object> userInfo(int userId,HttpServletRequest request) {
        Map<String,Object> map = new HashMap<>();
        User user = userService.getUserById(userId);
        map.put("user",user);
        //检测登陆者是否为关注用户
        int myId = Integer.parseInt(MyUtils.getCookie(request, "id").getValue());
        boolean followedStatus = followService.isFollow(myId,userId);
        map.put("followedStatus",followedStatus);

        return map;
    }
}
